package com.nut2014.controller;

import com.nut2014.config.ConfigResource;
import com.nut2014.entity.User;
import com.nut2014.pojo.BaseResponse;
import com.nut2014.service.TokenService;
import com.nut2014.service.UserService;
import com.nut2014.utils.MyUtils;
import com.nut2014.verification.UserLoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {

    @Autowired
    private UserService dataService;

    @Autowired
    private TokenService tokenService;

    @RequestMapping("/login")
    public BaseResponse getUser(String userName, String passWord) {
        User user = dataService.getUser(userName);
        if (user != null) {
            String passStr = MyUtils.MD5encode(userName + passWord);
            if (user.getPassWord().equalsIgnoreCase(passStr)) {
                user.setToken(tokenService.getToken(user));
                dataService.update(user);
                return new BaseResponse<>(1, "登录成功", user);
            }
        }
        return new BaseResponse<>(0, "登录失败,用户名或密码错误", new User());

    }

    @Autowired
    ConfigResource configResource;

    @RequestMapping("/registered")
    public BaseResponse registered(String userName, String passWord) {
        User user = dataService.getUser(userName);
        if (user == null) {
            user = new User(userName, passWord, userName);
            user.setToken(tokenService.getToken(user));
            if (user.getAvatarPath() == null || user.getAvatarPath().isEmpty()) {
                user.setAvatarPath(configResource.getDefaultAvatarPath());
            }
            if (user.getBgImg() == null || user.getBgImg().isEmpty()) {
                user.setBgImg(configResource.getDefaulUserBg());
            }
            if (user.getRealName() == null || user.getRealName().isEmpty()) {
                user.setRealName(user.getUserName());
            }
            int sqlCode = dataService.add(user);
            if (sqlCode == 1) {
                user = dataService.getUser(userName);
            }
            return new BaseResponse<>(sqlCode, "注册成功", user);
        }
        return new BaseResponse<>(0, "注册失败,用户名已存在", new User());

    }
    @UserLoginToken
    @RequestMapping("/changePassword")
    public BaseResponse changePassword(String userName, String passWord, String newPassWord) {
        User user = dataService.getUser(userName);
        if (!passWord.equals(user.getPassWord())) {
            return new BaseResponse<>(0, "原密码错误", new User());
        } else {
            user.setPassWord(newPassWord);
            dataService.update(user);
            return new BaseResponse<>(1, "修改成功", new User());
        }

    }

    @UserLoginToken
    @RequestMapping("/outLogin")
    public BaseResponse outLogin(int userId) {
        User user = dataService.get(userId);
        user.setToken("");
        dataService.update(user);
        return new BaseResponse<>(1, "退出登录成功", new User());
    }

}
